<?php
// application/controllers/RelationController.php

/**
 * RelationController is the controller for handling relations
 *

 * @author Hermen van den Hoorn
 * @version $Id$
 */
class AttributeController extends Vakanz_CRM_Controller_Adminaction
{
    /**
     * The "index" action is the default action for all controllers.
     *
     * Assuming the default route and default router, this action is dispatched
     * via the following urls:
     *   /relation/
     *   /realation/index
     *
     * @return void
     */
	public function indexAction() {
		$attributes =new Vakanz_Attributes();
		$attributesRs = $attributes->fetchAll($attributes->select()->order('attribute_position'));
		$this->view->attributes = $attributesRs->toStdObjectArray();
	}


    public function newAction()
    {
        /*
           There is nothing inside this action, but it will still attempt to
           render a view.  This is because by default, the front controller
           uses the ViewRenderer action helper to handle auto rendering
           In the MVC grand scheme of things, the ViewRenderer allows us to
           draw the line between the C and V in the MVC.  Also note this action
           helper is optional, but on default.
        */
        $this->_forward('edit');
    }
    public function editAction() {
    	$attributes = new Vakanz_Attributes();
    	if ($this->_getParam('attribute_id')) {
    		$this->view->attribute = $attributes->fetchAll($attributes->select()->where('attribute_id = ?', $this->_getParam('attribute_id')))->current();
    	} else {
    		$this->view->attribute = $attributes->createRow();
    	}
		$forms = new General_Forms();
		$form = $forms->findByName('attribute');
		$form->getFormElements();
		$formObj = $form->toStdObject();
        $this->view->form = $formObj;

        $this->_helper->actionStack('search','relation');
    }
    public function searchAction() {
    	$this->render($this->getRequest()->getActionName(),$this->getRequest()->getActionName());
    }

    public function checkAction() {
    	$attributes = new Vakanz_Attributes();
    	if ($this->_getParam('attribute_id') == 0) {
    		$attribute = $attributes->createRow();
    	} else {
    		$query = $attributes->select()->where('attribute_id = ? ',$this->_getParam('attribute_id'));
    		$attribute = $attributes->fetchAll($query)->current();
    	}
    	$attribute->fillByParams($this->_request->getParams());
    	if ($this->_getParam('attribute_id') == 0) {
	    	if ($attribute->isExisting()) {
	    		$existing = $attribute->getExistingRelation();
	    		$this->view->attribute = $attribute;
	    		$this->view->existing_attribute = $existing;
	    	} else {
	    		$this->_forward('save',null,null,array('params' => $this->_request->getParams()));
	    	}
    	} else {
    		$this->_forward('save',null,null,array('params' => $this->_request->getParams()));
    	}

    }

    public function saveAction() {
    	$attributes = new Vakanz_Attributes();
    	if ($this->_getParam('attribute_id') == 0) {
    		$attribute = $attributes->createRow();
    	} else {
    		$query = $attributes->select()->where('attribute_id = ? ',$this->_getParam('attribute_id'));
    		$attribute = $attributes->fetchAll($query)->current();
    	}
    	$attribute->fillByParams($this->_request->getParams());

    	$attribute->save();

    }
}
